// miniprogram/pages/replacemodify/replace6_sche/sche.js
const db=wx.cloud.database();
const _ = db.command;
//时间转换函数
import common from "../../../utils/getMyData.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //下拉框显示
    getChiefIdToSche:"", show_chief_otherchief:false,  gender_chief_otherchief:"", //值班表页面的医师上拉框合一


    option3: [
      { text: '2018', value: 0 },
      { text: '2019', value: 1 },
      { text: '2020', value: 2 },
      { text: '2021', value: 3 },
      { text: '2022', value: 4 },
      { text: '2023', value: 5 },
      { text: '2024', value: 6 },
      { text: '2025', value: 7 },
      { text: '2026', value: 8 },
      { text: '2027', value: 9 },
      { text: '2028', value: 10 },
      { text: '2029', value: 11 },
    ],
    value3: 2,
    option4: [
      { text: '1', value: 0 },
      { text: '2', value: 0 },
      { text: '3', value: 0 },
      { text: '4', value: 0 },
      { text: '5', value: 0 },
      { text: '6', value: 0 },
      { text: '7', value: 0 },
      { text: '8', value: 0 },
      { text: '9', value: 0 },
      { text: '10', value: 0 },
      { text: '11', value: 0 },
      { text: '12', value: 0 },
    ],
    value4: 0,

    //值班表页面
    sche_group:"",
    sche_chief:"",
    sche_deputy_chief:"",
    sche_attend_doc:"",
    sche_resi_doc:"",
    sche_rota_doc:""
  },

  //输入框回调
  onChange(e) {
    //输入框
    console.log(e.detail);   
    this.setData({
      //因为按钮处要进行wx:if判断，所以要将所有输入框的数据声明
      [e.target.dataset.key]:e.detail
    })
    console.log(this.data)
  },

  //提交表单添加进数据库
  btnSub_sche(res){
    var {sche_group,sche_chief,sche_deputy_chief,sche_attend_doc,sche_resi_doc,sche_rota_doc,sche_discuss_time,sche_in_consul,sche_out_consul,sche_department,sche_desc,sche_rest_time1,sche_rest_time2,sche_pub_edu1,sche_pub_edu2,sche_month,sche_year}=res.detail.value
    if(sche_rest_time1=="") sche_rest_time1="许艺超"
    if(sche_rest_time2=="") sche_rest_time2="周末"
    if(sche_discuss_time=="") sche_discuss_time="必要时"
    if(sche_in_consul=="") sche_in_consul="当日值班二线"
    if(sche_out_consul=="") sche_out_consul="科主任或指定"
    if(sche_department=="") sche_department="主任轮流"
    if(1) sche_year+=2018
    if(1) sche_month+=1
    console.log(res.detail)
     //进入加载页面
     wx.showLoading({
      title: '加载中...',
      mask: true
      })
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
    wx.cloud.callFunction({
      name:"newAdd",
      data:{
        action:'sche',
        sche_month:sche_year+"-"+sche_month,
        sche_group:sche_group,
        //patient_id:this.data.getPatientIdToSs[ss_patient_id-1].patient_id, 
        sche_chief:this.data.sche_otherchief[0],
        sche_deputy_chief:this.data.sche_otherchief[1],
        sche_attend_doc:this.data.sche_otherchief[2],
        sche_resi_doc:this.data.sche_otherchief[3],
        sche_rota_doc:this.data.sche_otherchief[4],
        sche_rest_time:this.data.sche_otherchief[5]+"/"+sche_rest_time2,
        sche_discuss_time:sche_discuss_time,
        sche_pub_edu:this.data.sche_otherchief[6]+"/"+this.data.sche_otherchief[7],
        sche_in_consul:sche_in_consul,
        sche_out_consul:sche_out_consul,
        sche_department:sche_department,
        sche_desc:sche_desc,
        database_id:this.data.database_id
      }
    }).then(res=>{
      console.log(res)
      //清除填写内容
      this.setData({
        //清空value
        // value:"",
        // message:"",
        // gender_sche_otherchief:"",
        //清空值
        // sche_group:"",
        // sche_chief:"",
        // sche_deputy_chief:"",
        // sche_attend_doc:"",
        // sche_resi_doc:"",
        // sche_rota_doc:"",
        // sche_otherchief:""
      })
    })
  },

    //值班表页面医师上拉框组合
  showPopup_sche_otherchief(e){      //点击选择性别，打开弹出层（选择器）
    db.collection("user").where({
    }).get().then(res=>{
      this.setData({
        getChiefIdToSche:res.data,
        columns_sche_otherchief:[]
      })
      for(var i=0;i<res.data.length;i++){
        this.data.columns_sche_otherchief[i]=res.data[i].user_name
      }
      this.setData({
        columns_sche_otherchief:this.data.columns_sche_otherchief,
      })
    })
    this.data.show_sche_otherchief[e.currentTarget.dataset.index]=true
    this.setData({
      //columns_opertime: ['A','B','AB','O'],   //选择器中的值
      show_sche_otherchief:this.data.show_sche_otherchief
    })
  },
  onClose_sche_otherchief(e) {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    for(var i=0;i<this.data.show_sche_otherchief.length;i++){
      this.data.show_sche_otherchief[i]=false
    }
    this.data.show_sche_otherchief[e.currentTarget.dataset.index]=false
    this.setData({ show_sche_otherchief: this.data.show_sche_otherchief });
  },
  onConfirm_sche_otherchief(e){    //选择器右上角的确定，点击确定获取值
    this.data.show_sche_otherchief[e.currentTarget.dataset.index]=false
    this.data.gender_sche_otherchief[e.currentTarget.dataset.index]=e.detail.value
    this.data.sche_otherchief[e.currentTarget.dataset.index]=this.data.getChiefIdToSche[e.detail.index]._openid
    this.setData({
      gender_sche_otherchief:this.data.gender_sche_otherchief,
      sche_otherchief:this.data.sche_otherchief,
      show_sche_otherchief:this.data.show_sche_otherchief
    })
  },

  //值班表页面医师上拉框组合
  showPopup_sche_otherchief(e){      //点击选择性别，打开弹出层（选择器）
    db.collection("user").where({
    }).get().then(res=>{
      this.setData({
        getChiefIdToSche:res.data,
        columns_sche_otherchief:[]
      })
      for(var i=0;i<res.data.length;i++){
        this.data.columns_sche_otherchief[i]=res.data[i].user_name
      }
      this.setData({
        columns_sche_otherchief:this.data.columns_sche_otherchief,
      })
    })
    this.data.show_sche_otherchief[e.currentTarget.dataset.index]=true
    this.setData({
      //columns_opertime: ['A','B','AB','O'],   //选择器中的值
      show_sche_otherchief:this.data.show_sche_otherchief
    })
  },
  onClose_sche_otherchief(e) {     //点击空白处开闭弹出层（选择器）及选择器左上角的取消
    for(var i=0;i<this.data.show_sche_otherchief.length;i++){
      this.data.show_sche_otherchief[i]=false
    }
    this.data.show_sche_otherchief[e.currentTarget.dataset.index]=false
    this.setData({ show_sche_otherchief: this.data.show_sche_otherchief });
  },
  onConfirm_sche_otherchief(e){    //选择器右上角的确定，点击确定获取值
    this.data.show_sche_otherchief[e.currentTarget.dataset.index]=false
    this.data.gender_sche_otherchief[e.currentTarget.dataset.index]=e.detail.value
    this.data.sche_otherchief[e.currentTarget.dataset.index]=this.data.getChiefIdToSche[e.detail.index]._openid
    this.setData({
      gender_sche_otherchief:this.data.gender_sche_otherchief,
      sche_otherchief:this.data.sche_otherchief,
      show_sche_otherchief:this.data.show_sche_otherchief
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //值班表页面的医师（多输入框合一）
    var sche_otherchief=["","","","","","","",""]  //数组
    var show_sche_otherchief=["","","","","","","",""]  //数组
    var gender_sche_otherchief=["","","","","","","",""]  //数组
    this.setData({
      sche_otherchief:sche_otherchief,
      show_sche_otherchief:show_sche_otherchief,
      gender_sche_otherchief:gender_sche_otherchief,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取患者数据 
    console.log(this.options)
    wx.cloud.callFunction({
      name: 'newModify', 
      data:({ 
        action:'sche_get',
        id: this.options.id.toString()
      }) 
   }).then(res => {
      this.setData({ 
       database_id: this.options.id.toString(),
       record: res.result.data[0],
       gender_patient_sex: res.result.data[0].patient_sex,
       gender_sche_otherchief:[res.result.data[0].sche_chief,res.result.data[0].sche_deputy_chief,res.result.data[0].sche_attend_doc,res.result.data[0].sche_resi_doc,res.result.data[0].sche_rota_doc,,,,],
       gender_sche_discuss_time:res.result.data[0].sche_deputy_chief,
       gender_sche_in_consul:res.result.data[0].sche_in_consul,
       gender_sche_out_consul:res.result.data[0].sche_out_consul,
       gender_sche_department:res.result.data[0].sche_department,
       gender_sche_desc:res.result.data[0].sche_desc,

       sche_otherchief:[res.result.data[0].sche_chief,res.result.data[0].sche_deputy_chief,res.result.data[0].sche_attend_doc,res.result.data[0].sche_resi_doc,res.result.data[0].sche_rota_doc,,,,],
       sche_discuss_time:res.result.data[0].sche_deputy_chief,
       sche_in_consul:res.result.data[0].sche_in_consul,
       sche_out_consul:res.result.data[0].sche_out_consul,
       sche_department:res.result.data[0].sche_department,
       sche_desc:res.result.data[0].sche_desc,

  
      //  sche_department:sche_department,
      //  sche_desc:sche_desc,
      }) 
      console.log(res.result.data)
      console.log(this.options)
      //获取数据 
      wx.hideLoading() 
   })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})